home *** CD-ROM | disk | FTP | other *** search
/ Aminet 48 / Aminet 48 (2002)(GTI - Schatztruhe)[!][Apr 2002].iso / Aminet / util / moni / Scout-src.lha / source / scout_net.c < prev    next >
Encoding:
C/C++ Source or Header  |  2002-02-13  |  20.7 KB  |  791 lines

  1. /**
  2.  * Scout - The Amiga System Monitor
  3.  *
  4.  *------------------------------------------------------------------
  5.  *
  6.  * This program is free software; you can redistribute it and/or modify
  7.  * it under the terms of the GNU General Public License as published by
  8.  * the Free Software Foundation; either version 2 of the License, or
  9.  * any later version.
  10.  *
  11.  * This program is distributed in the hope that it will be useful,
  12.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  14.  * GNU General Public License for more details.
  15.  *
  16.  * You should have received a copy of the GNU General Public License
  17.  * along with this program; if not, write to the Free Software
  18.  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  19.  *
  20.  * You must not use this source code to gain profit of any kind!
  21.  *
  22.  *------------------------------------------------------------------
  23.  *
  24.  * @author Andreas Gelhausen
  25.  * @author Richard Körber <rkoerber@gmx.de>
  26.  */
  27.  
  28.  
  29.  
  30. #include "system_headers.h"
  31.  
  32. extern APTR    AP_Scout;
  33. extern STRPTR  _ProgramName;
  34.  
  35. /********************************************************************/
  36. /*                      Benötigte Prototypes                        */
  37. /********************************************************************/
  38.  
  39. struct Library *MyOpenLibrary (char *, ULONG);
  40.  
  41. void PrintAllocations(char *);
  42. void PrintAssigns(char *);
  43. void PrintCommands(char *);
  44. void PrintDevices(char *);
  45. void PrintExpansions(char *);
  46. void PrintFonts(char *);
  47. void PrintInputHandlers(char *);
  48. void PrintInterrupts(char *);
  49. void PrintLibraries(char *);
  50. void PrintMemory(char *);
  51. void PrintMounts(char *);
  52. void PrintLocks(char *);
  53. void PrintPorts(char *);
  54. void PrintResidents(char *);
  55. void PrintSemaphores(char *);
  56. void PrintTasks(char *);
  57. void PrintResources(char *);
  58. void PrintVectors(char *);
  59. void PrintWindows(char *);
  60. void PrintLowMemory(char *);
  61. void PrintCx(char *);
  62. void PrintClass(char *);
  63. void PrintSMode(char *);
  64. void PrintSystem(char *);
  65. void PrintTimer(char *);
  66.  
  67. void SendAlcList(void);
  68. void SendAssList(void);
  69. void SendComList(void);
  70. void SendDevList(void);
  71. void SendExpList(void);
  72. void SendFontList(void);
  73. void SendInputList(void);
  74. void SendIntList(void);
  75. void SendLibList(void);
  76. void SendLockList(void);
  77. void SendMemList(void);
  78. void SendMountList(void);
  79. void SendPortList(void);
  80. void SendResList(void);
  81. void SendResiList(void);
  82. void SendSemList(void);
  83. void SendTaskList(void);
  84. void SendVectorList(void);
  85. void SendWinList(void);
  86. void SendLowMemory(void);
  87. void SendCxList(void);
  88. void SendClassList(void);
  89. void SendSModeList(void);
  90. void SendSystemList(void);
  91. void SendTimerList(void);
  92.  
  93. void PrintNetHelp (void);
  94. long SendResultString (char *fmt, ...);
  95. int MyRequest (char *gadgets, char *fmt, ...);
  96.  
  97. #define NAMELEN      32
  98. #define LINELEN      82
  99. #define TRENNCHAR    '\0'
  100.  
  101. #define aprintf      Printf
  102.  
  103. char  *CMD_BEGIN = "BEGIN";
  104. char  *CMD_USER = "USER";
  105. char  *CMD_PASSWORD = "PASSWORD";
  106. char  *CMD_END = "END";
  107. char  *CMD_DONE = "DONE";
  108. char  *CMD_ERROR = "ERROR";
  109. char  *CMD_CONNECTED = "CONNECTED";
  110.  
  111. static struct  RDArgs      *rdargs;
  112. static struct  RDArgs      *myrdargs;
  113.  
  114. int     server_socket = -1;
  115. int     client_socket = -1;
  116. int     connected = FALSE;
  117. static long    type = SOCK_STREAM;
  118.  
  119. static long    sinlen;
  120. static struct  sockaddr_in sin;
  121.  
  122. BOOL clientstate = FALSE;
  123. BOOL serverstate = FALSE;
  124. BOOL shellstate = FALSE;
  125.  
  126. char username[10+1];
  127. char password[_PASSWORD_LEN+1];
  128.  
  129.  
  130. /*
  131. int logprint (char *fmt, ...) {
  132.    int   rc = TRUE;
  133.    char  buf[100];
  134.    char  *logfilename = "basket:scout.log";
  135.    BPTR  handle;
  136.  
  137.    va_list args;
  138.    va_start(args,fmt);
  139.    _sprintf (buf, fmt, args);
  140.  
  141.    if (((handle = Open (logfilename, MODE_OLDFILE)) \
  142.      && (Seek (handle, 0, OFFSET_END) != -1)) \
  143.      || (handle = Open (logfilename, MODE_NEWFILE))) {
  144.       if (Write (handle, buf, strlen (buf)) == -1) {
  145.          rc = FALSE;
  146.       }
  147.       Close (handle);
  148.    }
  149.    va_end (args);
  150.    return (rc);
  151. }
  152. */
  153.  
  154. void failtcp (void) {
  155.    if (client_socket >= 0) {
  156.       if (connected) {
  157.          shutdown (client_socket, 2);
  158.       }
  159.       CloseSocket (client_socket);
  160.    }
  161.  
  162. //   _STD_500_stdio_term();
  163.  
  164.    if (UserGroupBase) CloseLibrary (UserGroupBase);
  165.  
  166. //   _STD_200_closeTimer();
  167.  
  168.    if (SocketBase) CloseLibrary (SocketBase);
  169. }
  170.  
  171. int inittcp (void) {
  172.    SocketBase = OpenLibrary ("bsdsocket.library", 4L);
  173.    if (! SocketBase)
  174.       return (FALSE);
  175.  
  176. //   _STI_200_openTimer();
  177.  
  178.    UserGroupBase = OpenLibrary ("usergroup.library", 4);
  179.    if (! UserGroupBase)
  180.       return (FALSE);
  181.  
  182. //   _STI_500_stdio_init();
  183. //   _STI_510_install_AmiTCP_callback();
  184.  
  185.    return (TRUE);
  186. }
  187.  
  188. static int sgetc (int sock) {
  189.    unsigned char c;
  190.    fd_set rd,ex;
  191.    ULONG flgs;
  192.    int n;
  193.  
  194.    struct timeval t;
  195.    t.tv_sec = 10L;
  196.    t.tv_usec = 0;
  197.  
  198.    FD_ZERO(&rd);
  199.    FD_ZERO(&ex);
  200.  
  201.    FD_SET(sock,&rd);
  202.    FD_SET(sock,&ex);
  203.  
  204.    if (clientstate) {
  205.       flgs = 0;
  206.       WaitSelect(16,&rd,0L,&ex,&t,&flgs);
  207.    } else {
  208.       flgs = SIGBREAKF_CTRL_C | SIGBREAKF_CTRL_D;
  209.       WaitSelect(16,&rd,0L,&ex,NULL,&flgs);
  210.    }
  211.  
  212.    if (FD_ISSET(sock,&rd)) {
  213.       n = recv(sock, &c, 1, 0);
  214.       if (n == 1) {
  215.          return c;
  216.       } else {
  217.          return -1;
  218.       }
  219.    } else {
  220.       return -1;
  221.    }
  222. }
  223.  
  224. int sgets (int sock, char *string, int maxchars) {
  225.    int   nchars = 0;
  226.    char  c;
  227.  
  228.    while ((nchars < maxchars) && ((c = sgetc(sock)) != EOF) \
  229.      && (c != TRENNCHAR)) {
  230.       if ((!clientstate) && (c == '\n')) {
  231. //         break;
  232.       }
  233.       string[nchars] = c;
  234.       nchars++;
  235.    }
  236.    string[nchars] = '\0';
  237.    return (nchars);
  238. }
  239.  
  240. #if 0
  241. static char *GetDate (void) {
  242.    time_t   clock = time (NULL);
  243.    return (ctime (&clock));
  244. }
  245. #endif
  246.  
  247. /*
  248. static int StripCRLF (char *buffer) {
  249.    int len = strlen (buffer);
  250.    
  251.    while (len && ((buffer[len-1] == '\n') \
  252.      || (len && (buffer[len-1] == '\r')))) {
  253.       buffer[--len] = '\0';
  254.    }
  255.    return (len);
  256. }
  257. */
  258.  
  259. static char    NetHelpText[] = "\n" \
  260.    " Available lists:\n" \
  261.    " ----------------\n" \
  262.    " Allocations     Assigns         BoopsiClasses   Commands\n" \
  263.    " Commodities     Devices         Expansions      Fonts\n" \
  264.    " InputHandlers   Interrupts      Libraries       Locks\n" \
  265.    " LowMemory       Memory          Mounts          Ports\n" \
  266.    " Residents       Resources       ScreenMode      Semaphores\n" \
  267.    " System          Tasks           Timer           Vectors\n" \
  268.    " Windows\n\n" \
  269.    " Other commands:\n" \
  270.    " ---------------\n" \
  271.    " Help";
  272.  
  273. void PrintNetHelp (void)
  274. {
  275.    if (serverstate || shellstate) {
  276.       int i = 0;
  277.       UBYTE *tmpstr;
  278.  
  279.       if (tmpstr = AllocVec(256, MEMF_ANY)) {
  280.          SendResultString (NetHelpText);
  281.  
  282.          while (arexx_list[i].mc_Name) {
  283.             strcpy (tmpstr, " ");
  284.             strcat (tmpstr, arexx_list[i].mc_Name);
  285.             if (arexx_list[i].mc_Parameters) {
  286.                strcat (tmpstr, " ");
  287.                strcat (tmpstr, arexx_list[i].mc_Template);
  288.             }
  289.             SendResultString (tmpstr);
  290.             i++;
  291.          }
  292.  
  293.          FreeVec(tmpstr);
  294.       }
  295.    }
  296. }
  297.  
  298. /*
  299. ** Dieser Hook wird benötigt um die ARexx-Command-Liste für
  300. **  die TCP_Befehle nutzen zu können!
  301. */
  302. struct NetHook {
  303.    struct   MinNode  h_egal1;
  304.    __asm    ULONG    (*h_Entry) (register __a1 ULONG *);
  305.    ULONG    (*h_egal2)();
  306.    APTR     h_egal3;
  307. };
  308.  
  309. struct Net_Command {
  310.    char  *nc_Name;
  311.    void  (*nc_func) (char *);
  312. };
  313.  
  314. struct Net_Command net_list[] = {
  315.    "Allocations",    PrintAllocations,
  316.    "Assigns",        PrintAssigns,
  317.    "BoopsiClasses",  PrintClass,
  318.    "Commands",       PrintCommands,
  319.    "Commodities",    PrintCx,
  320.    "Devices",        PrintDevices,
  321.    "Expansions",     PrintExpansions,
  322.    "Fonts",          PrintFonts,
  323.    "InputHandlers",  PrintInputHandlers,
  324.    "Interrupts",     PrintInterrupts,
  325.    "Libraries",      PrintLibraries,
  326.    "LowMemory",      PrintLowMemory,
  327.    "Memory",         PrintMemory,
  328.    "Mounts",         PrintMounts,
  329.    "Locks",          PrintLocks,
  330.    "Ports",          PrintPorts,
  331.    "Residents",      PrintResidents,
  332.    "ScreenMode",     PrintSMode,
  333.    "Semaphores",     PrintSemaphores,
  334.    "System",         PrintSystem,
  335.    "Tasks",          PrintTasks,
  336.    "Timer",          PrintTimer,
  337.    "Resources",      PrintResources,
  338.    "Vectors",        PrintVectors,
  339.    "Windows",        PrintWindows,
  340.    "Help",           (void (* ) (char *)) PrintNetHelp,
  341.    "a",              PrintAllocations,
  342.    "b",              PrintClass,
  343.    "c",              PrintCommands,
  344.    "d",              PrintDevices,
  345.    "e",              PrintTimer,
  346.    "f",              PrintFonts,
  347.    "g",              PrintAssigns,
  348.    "h",              PrintInputHandlers,
  349.    "i",              PrintInterrupts,
  350.    "j",              PrintLowMemory,
  351.    "k",              PrintCx,
  352.    "l",              PrintLibraries,
  353.    "m",              PrintMemory,
  354.    "n",              PrintMounts,
  355.    "o",              PrintLocks,
  356.    "p",              PrintPorts,
  357.    "r",              PrintResidents,
  358.    "s",              PrintSemaphores,
  359.    "t",              PrintTasks,
  360.    "u",              PrintResources,
  361.    "v",              PrintVectors,
  362.    "w",              PrintWindows,
  363.    "x",              PrintExpansions,
  364.    "y",              PrintSystem,
  365.    "z",              PrintSMode,
  366.    "GetAlcList",     (void (* ) (char *)) SendAlcList,
  367.    "GetAssList",     (void (* ) (char *)) SendAssList,
  368.    "GetClassList",   (void (* ) (char *)) SendClassList,
  369.    "GetComList",     (void (* ) (char *)) SendComList,
  370.    "GetCxList",      (void (* ) (char *)) SendCxList,
  371.    "GetDevList",     (void (* ) (char *)) SendDevList,
  372.    "GetExpList",     (void (* ) (char *)) SendExpList,
  373.    "GetFontList",    (void (* ) (char *)) SendFontList,
  374.    "GetInputList",   (void (* ) (char *)) SendInputList,
  375.    "GetIntList",     (void (* ) (char *)) SendIntList,
  376.    "GetLibList",     (void (* ) (char *)) SendLibList,
  377.    "GetLockList",    (void (* ) (char *)) SendLockList,
  378.    "GetLowMemList",  (void (* ) (char *)) SendLowMemory,
  379.    "GetMemList",     (void (* ) (char *)) SendMemList,
  380.    "GetMountList",   (void (* ) (char *)) SendMountList,
  381.    "GetPortList",    (void (* ) (char *)) SendPortList,
  382.    "GetResList",     (void (* ) (char *)) SendResList,
  383.    "GetResiList",    (void (* ) (char *)) SendResiList,
  384.    "GetSemList",     (void (* ) (char *)) SendSemList,
  385.    "GetSModeList",   (void (* ) (char *)) SendSModeList,
  386.    "GetSysList",     (void (* ) (char *)) SendSystemList,
  387.    "GetTaskList",    (void (* ) (char *)) SendTaskList,
  388.    "GetTimerList",   (void (* ) (char *)) SendTimerList,
  389.    "GetVectorList",  (void (* ) (char *)) SendVectorList,
  390.    "GetWinList",     (void (* ) (char *)) SendWinList,
  391.    NULL
  392. };
  393.  
  394. /********************************************************************/
  395. /*                              Client                              */
  396. /********************************************************************/
  397.  
  398. int SendDaemon (char *fmt, ...) {
  399.    int   result = FALSE;
  400.    char  buf[LINELEN];
  401.  
  402.    va_list args;
  403.    va_start (args,fmt);
  404.    RawDoFmt(fmt, args, (void (*)())"\x16\xc0\x4e\x75", buf);
  405.  
  406.    if (send (client_socket, buf, strlen (buf) + 1, 0) == strlen (buf) + 1) { 
  407.       result = TRUE;
  408.    }
  409.    va_end (args);
  410.    return (result);
  411. }
  412.  
  413. static ULONG NetCommand (char *text) {
  414.    ULONG rc = RETURN_FAIL;
  415.    char  buf[LINELEN];
  416.  
  417.    if (send (client_socket, text, strlen (text) + 1, 0) == strlen (text) + 1) { 
  418. // aprintf ("COMMAND '%s' send\n", text);
  419.       while (sgets (client_socket, buf, LINELEN)) {
  420.          if (strcmp (buf, CMD_DONE) == 0) {
  421.             rc = RETURN_OK;
  422. // aprintf ("DONE\n");
  423.             break;
  424.          } else if (strcmp (buf, CMD_ERROR) == 0) {
  425. // aprintf ("ERROR\n");
  426.             break;
  427.          }
  428.       }
  429.    }
  430.    return (rc);
  431. }
  432.  
  433. int ReceiveDecodedEntry (UBYTE *structure, int length) {
  434.    int   rc = FALSE, i = 8;
  435.  
  436.    if (sgets (client_socket, structure + 8, length - 8)) {
  437.       if ((strcmp (structure + 8, CMD_ERROR)) \
  438.         && (strcmp (structure + 8, CMD_DONE))) {
  439.          while (i < length - 1) {
  440.             if (structure[i] == '\1')
  441.                structure[i] = '\0';
  442.             i++;
  443.          }
  444.          rc = TRUE;
  445.       }
  446.    }
  447.    return (rc);
  448. }
  449.  
  450. int ConnectToServer (void) {
  451.    char     hostname[NAMELEN];
  452.    char     buffer[LINELEN+2];
  453. //   struct   servent     *service;
  454.    struct   hostent     *host;
  455.    struct   sockaddr_in server;
  456.  
  457.    if (! inittcp()) {
  458.       aprintf ("Failed to initialize AmiTCP version 4!\n");
  459.       return (FALSE);
  460.    }
  461.  
  462.    if (opts.User) {
  463.       strncpy (username, opts.User, 10);
  464.    } else {
  465.       strncpy (username, getlogin(), 10);
  466.    }
  467.  
  468.    if (opts.Password) {
  469.       strncpy (password, opts.Password, _PASSWORD_LEN);
  470.    } else {
  471.       strncpy (password, getpass ("Password:"), _PASSWORD_LEN);
  472.       if (password[0] == '\0') {
  473.          aprintf ("Failed to get password!\n");
  474.          return (FALSE);
  475.       }
  476.    }
  477.  
  478.    gethostname (hostname, sizeof (hostname));
  479. //   service = getservbyname ("scout","tcp");
  480.  
  481.    if (host = gethostbyname (opts.Host)) {
  482.  
  483.       bzero ((char *) &server, sizeof(server));
  484. //      server.sin_port = service->s_port;
  485.       server.sin_port = 6543;
  486.       bcopy (host->h_addr, (char *) &server.sin_addr, host->h_length);
  487.       server.sin_family = host->h_addrtype;
  488.  
  489.       client_socket = socket (AF_INET, type, 0);
  490.       if (client_socket >= 0) {
  491.          if (connect (client_socket, (struct sockaddr *) &server, sizeof (server)) != -1) {
  492.             connected = TRUE;
  493.             if (SendDaemon (CMD_BEGIN)) {
  494.                if ((SendDaemon ("%s %s", CMD_USER, username)) \
  495.                  && (SendDaemon ("%s %s", CMD_PASSWORD, password))) {
  496.  
  497.                   if ((sgets (client_socket, buffer, LINELEN)) \
  498.                     && (strcmp (buffer, CMD_CONNECTED))) {
  499.                      aprintf (buffer);
  500.                   } else {
  501.                      return (TRUE);
  502.                   }
  503.                }
  504.             }
  505.          } else {
  506.             aprintf ("Couldn't connect to server\n");
  507.          }
  508.       } else {
  509.          aprintf ("Couldn't create socket\n");
  510.       }
  511.    } else {
  512.       aprintf ("Host is not available\n");
  513.    }
  514.    return (FALSE);
  515. }
  516.  
  517. ULONG netshellclient (VOID) {
  518.    ULONG    rc = RETURN_FAIL;
  519.    char     recvbuffer[LINELEN+2];
  520.  
  521.    if (clientstate = ConnectToServer()) {
  522.       if (SendDaemon ("%s ", opts.Command)) {
  523.  
  524.          while ((sgets (client_socket, recvbuffer, LINELEN)) \
  525.            && (strcmp (recvbuffer, CMD_ERROR)) \
  526.            && (strcmp (recvbuffer, CMD_DONE))) {
  527.             aprintf (recvbuffer);
  528.          }
  529.          if (strcmp (recvbuffer, CMD_DONE) == 0) {
  530.             rc = RETURN_OK;
  531.          } else {
  532.             rc = RETURN_ERROR;
  533.          }
  534.          SendDaemon ("END");
  535.       }
  536.    }
  537.    return (rc);
  538. }
  539.  
  540. /********************************************************************/
  541. /*                              Daemon                              */
  542. /********************************************************************/
  543.  
  544. int isNetCall (void) {
  545.    int   rc = TRUE;
  546.  
  547.    if (! inittcp()) {
  548.       return (FALSE);
  549.    }
  550.  
  551.    server_socket = init_inet_daemon();
  552.    if (server_socket >= 0) {
  553.       set_socket_stdio (server_socket);
  554.       sinlen = sizeof (sin);
  555.       if (getpeername (0, (struct sockaddr *) &sin, &sinlen) == -1) {
  556. //         logprint ("scout: getpeername() failed\n");
  557.          rc = FALSE;
  558.       }
  559.    } else {
  560. //      logprint ("scout: init_inet_daemon() failed\n");
  561.       rc = FALSE;
  562.    }
  563.    return (rc);
  564. }
  565.  
  566. int isCommand (char *buffer, char *command) {
  567.    int len = strlen (command);
  568.    int rc = FALSE;
  569.  
  570.    if ((strnicmp (buffer, command, len) == 0) \
  571.      && ((isspace (buffer[len])) || (buffer[len] == '\0'))) {
  572.       rc = len + 1;
  573.    }
  574.    return (rc);
  575. }
  576.  
  577. int SendClient (char *fmt, ...) {
  578.    int   result = FALSE;
  579.    char  buf[2048];
  580.  
  581.    va_list args;
  582.    va_start(args,fmt);
  583.    RawDoFmt(fmt, args, (void (*)())"\x16\xc0\x4e\x75", buf);
  584.  
  585.    if (send (0, buf, strlen (buf) + 1, 0) == strlen (buf) + 1) { 
  586.       result = TRUE;
  587.    }
  588.    va_end (args);
  589.    return (result);
  590. }
  591.  
  592. int SendEncodedEntry (UBYTE *structure, int length) {
  593.    int   i = 8;
  594.  
  595.    while (i < length - 1) {
  596.       if (structure[i] == '\0')
  597.          structure[i] = '\1';
  598.       i++;
  599.    }
  600.    if (SendClient (structure + 8)) { 
  601.       return (FALSE);
  602.    }
  603.    return (TRUE);
  604. }
  605.  
  606. long SendResultString (char *fmt, ...) {
  607.    char  buf[2048];
  608.  
  609.    va_list args;
  610.    va_start (args,fmt);
  611.    RawDoFmt(fmt, args, (void (*)())"\x16\xc0\x4e\x75", buf);
  612.  
  613.    if (serverstate) {
  614.       strcat (buf, "\n");
  615.       SendClient (buf);
  616.    } else if (shellstate) {
  617.       strcat (buf, "\n");
  618.       Printf (buf);
  619.    } else {
  620.       set (AP_Scout, MUIA_Application_RexxString, buf);
  621.    }
  622.    va_end (args);
  623.    return (TRUE);
  624. }
  625.  
  626. int PrintOneLine (BPTR hd, char *text) {
  627.    if (serverstate) {
  628.       return (SendClient (text));
  629.    } else {
  630.       return (Write (hd, text, strlen (text)) != -1);
  631.    }
  632. }
  633.  
  634. ULONG ExecuteCommand (char *text) {
  635.    ULONG    rc = RETURN_OK;
  636.    char     buffer[256];
  637.    int      i, len;
  638.  
  639.    #define  CMDOPT_TEMPLATE "CMD1/A"
  640.    #define  CMDOPT_COUNT    4
  641.    LONG     cmdopts[CMDOPT_COUNT];
  642.  
  643.    _sprintf (buffer, "%s ", text);
  644.  
  645.    i = 0;
  646.    while (arexx_list[i].mc_Name) {
  647.       if (len = isCommand (buffer, arexx_list[i].mc_Name)) {
  648.  
  649.          if (myrdargs = AllocDosObject (DOS_RDARGS,NULL)) {
  650.             myrdargs->RDA_Source.CS_Buffer = buffer + len;
  651.             myrdargs->RDA_Source.CS_Length = strlen (buffer) - len;
  652.  
  653.             if ((arexx_list[i].mc_Parameters && (rdargs = ReadArgs (arexx_list[i].mc_Template, (LONG *) &cmdopts, myrdargs))) \
  654.               || (!arexx_list[i].mc_Parameters)) {
  655.  
  656.                rc = (*((struct NetHook *) arexx_list[i].mc_Hook)->h_Entry) ((ULONG *) &cmdopts[0]);
  657.                FreeArgs (rdargs);
  658.             } else {
  659.  
  660.                Fault (IoErr(), NULL, (char *) buffer, LINELEN);
  661.                SendResultString (buffer);
  662.                rc = RETURN_FAIL;
  663.             }
  664.             FreeDosObject (DOS_RDARGS,myrdargs);
  665.          }
  666.          break;
  667.       }
  668.       i++;
  669.    }
  670.  
  671.    if (! (arexx_list[i].mc_Name)) {
  672.       i = 0;
  673.       while (net_list[i].nc_Name) {
  674.          if (len = isCommand (buffer, net_list[i].nc_Name)) {
  675.  
  676.             if (myrdargs = AllocDosObject (DOS_RDARGS,NULL)) {
  677.                myrdargs->RDA_Source.CS_Buffer = buffer;
  678.                myrdargs->RDA_Source.CS_Length = strlen (buffer);
  679.  
  680.                if (rdargs = ReadArgs (CMDOPT_TEMPLATE, (LONG *) &cmdopts, myrdargs)) {
  681.  
  682.                   (*net_list[i].nc_func) (NULL);
  683.                   FreeArgs (rdargs);
  684.                } else {
  685.  
  686.                   Fault (IoErr(), NULL, (char *) buffer, LINELEN);
  687.                   SendResultString (buffer);
  688.                   rc = RETURN_FAIL;
  689.                }
  690.                FreeDosObject (DOS_RDARGS,myrdargs);
  691.             }
  692.             break;
  693.          }
  694.          i++;
  695.       }
  696.       if (! (net_list[i].nc_Name)) {
  697.          SendResultString ("Unknown option");
  698.          rc = RETURN_FAIL;
  699.       }
  700.    }
  701.    va_end (args);
  702.    return (rc);
  703. }
  704.  
  705. ULONG netdaemon (VOID) {
  706.    ULONG    rc = TRUE;
  707.    char     buffer[LINELEN+2];
  708.    int      len, done = FALSE;
  709.  
  710.    struct   passwd   *pw, *rootpw;
  711.    long     rootid = 0;
  712.  
  713. /*
  714. ** inittcp() wurde bereits durch isNetCall() aufgerufen!
  715. */
  716.  
  717.    serverstate = TRUE;
  718.  
  719.    if (rootpw = getpwnam ("root"))
  720.       rootid = rootpw->pw_gid;
  721.  
  722.    if ((sgets (0, buffer, LINELEN)) && (strcmp (buffer, CMD_BEGIN) == 0)) {
  723. //logprint ("BEGIN\n");
  724.       if ((sgets (0, buffer, LINELEN)) && (len = isCommand (buffer, CMD_USER)) \
  725.         && (pw = getpwnam (buffer + len))) {
  726. //logprint ("USER %s\n", buffer + len);
  727.  
  728.          if (((rootpw) && (rootid == pw->pw_gid)) || (! rootpw)) {
  729.  
  730.             if ((sgets (0, buffer, LINELEN)) && (len = isCommand (buffer, CMD_PASSWORD))
  731.               && (strcmp (crypt (buffer + len, pw->pw_passwd), pw->pw_passwd) == 0)) {
  732. //logprint ("PASSWORD %s\n", buffer + len);
  733.  
  734.                if (SendClient (CMD_CONNECTED)) {
  735. //logprint ("'CONNECTED' send!\n");
  736.                   while (sgets (0, buffer, LINELEN)) {
  737. //logprint ("COMMAND '%s'\n", buffer);
  738.                      if (strcmp (buffer, CMD_END) == 0) {
  739.                         break;
  740.                      }
  741.  
  742.                      if (ExecuteCommand (buffer)) {
  743.                         SendClient (CMD_ERROR);
  744.                      } else {
  745.                         SendClient (CMD_DONE);
  746.                      }
  747.                      done = TRUE;
  748.                   }
  749.                }
  750.             } else {
  751.                SendClient ("Wrong password\n");
  752.             }
  753.          } else {
  754.             SendClient ("No root privileges\n");
  755.          }
  756.       } else {
  757.          SendClient ("Unknown user\n");
  758.       }
  759.    } else {
  760.       SendClient ("No 'BEGIN' received\n");
  761.    }
  762.    if (! done)
  763.       SendClient (CMD_ERROR);
  764.  
  765.    serverstate = FALSE;
  766.    return (rc);
  767. }
  768.  
  769. int MyDoCommand (char *fmt, ...) {
  770.    int   result = FALSE;
  771.    char  buf[LINELEN];
  772.  
  773.    va_list args;
  774.    va_start(args,fmt);
  775.    RawDoFmt(fmt, args, (void (*)())"\x16\xc0\x4e\x75", buf);
  776.  
  777.    if (clientstate) {
  778.       if (! NetCommand (buf))
  779.          result = TRUE;
  780.    } else {
  781.       if (! ExecuteCommand (buf))
  782.          result = TRUE;
  783.    }
  784.    if (! result) {
  785.       MyRequest ("Continue", "Error occured!");
  786.    }
  787.    va_end (args);
  788.    return (result);
  789. }
  790.  
  791.